package com.lu.adog.demo.main;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.widget.Toast;

import com.lu.adog.R;
import com.lu.adog.demo.base.App;
import com.lu.adog.demo.base.BaseConfig;
import com.lu.adog.demo.common.CommonActivity;
import com.lu.adog.demo.main.data.MainTask;

/**
 * @author Yue
 * @date 2017/11/13 17:44
 */
public class MainListActivity extends CommonActivity implements ILinkMain.View {

    private ILinkMain.Presenter presenter;
    private MainFragment mainFragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new MainPresenter().bind(this, new MainTask())
                .start();

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentByTag(currFragmentFlag);
        if (fragment != null) {
            setContentPage(fragment);
        } else {
            mainFragment = new MainFragment();
            setContentPage(mainFragment);
        }
    }

    private static final String currFragmentFlag = "contentPage";

    @Override
    public void setContentPage(final Fragment fragment) {
        final FragmentTransaction bt = getSupportFragmentManager().beginTransaction();
        Fragment currFragment = getSupportFragmentManager().findFragmentByTag(currFragmentFlag);
        if (fragment == currFragment) {
            return;
        }
        PropertyValuesHolder pvhAlpha = PropertyValuesHolder.ofFloat("alpha",0.6f,0.9f,1f);
        PropertyValuesHolder pvhTranX = PropertyValuesHolder.ofFloat("translationX",300,-100,0);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.fl_conent_main),pvhAlpha,pvhTranX);

        objectAnimator.setDuration(800);
        objectAnimator.start();

        bt.replace(R.id.fl_conent_main, fragment, currFragmentFlag);
        bt.commit();
        Bundle bundle = fragment.getArguments();
        int title = 0;
        if (bundle != null) {
            title = bundle.getInt(BaseConfig.KEY_TITLE);
        }
        setAcionBarTitle(title);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Fragment fragment = getSupportFragmentManager().findFragmentByTag(currFragmentFlag);
            if (fragment instanceof MainFragment) {
                presenter.onQuickClickExit();
            } else {
                setContentPage(mainFragment);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void exitAlertMess() {
        Toast.makeText(this, "再按一次退出应用", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void exitApp() {
        //动画
        App.getApp().exit();
    }

    @Override
    public void setPresenter(ILinkMain.Presenter presenter) {
        this.presenter = presenter;
    }
}
